package cn.com.bdqn.controller;

import cn.com.bdqn.entity.Orders;
import cn.com.bdqn.service.OrdersService;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/orders")
@Api(tags = "订单管理接口")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Operation(summary = "根据客户编号查询相关订单信息")
    @PostMapping("/queryByOdrCustomer")
    public ReturnData queryByOdrCustomer(String odrCustomer, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Orders> orders = ordersService.queryByOdrCustomer(odrCustomer);
            PageInfo pageInfo = new PageInfo(orders,3);
            return ReturnData.ok().put("orders",orders).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("数据库异常");
        }
    }

    @Operation(summary = "根据订单编号查询单个订单信息")
    @PostMapping("/queryByOdrCode")
    public ReturnData queryByOdrCode(String odrCode){
        try {
            Orders orders = ordersService.queryByOdrCode(odrCode);
            return ReturnData.ok().put("orders",orders);
        }catch (Exception e){
            return ReturnData.currError("数据库异常");
        }
    }
}
